package com.guigu.eduservice.service.impl;

import com.guigu.eduservice.service.CaptchaService;
import com.guigu.eduservice.utils.RedisUtils;
import com.guigu.eduservice.vo.CaptchaVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Author: janecraft
 * @Date: 2025/6/28
 * @Description: com.guigu.aclservice.service.impl
 * @Version： 1.0
 */
@Service
public class CaptchaServiceImpl implements CaptchaService {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public CaptchaVo cacheCaptcha(String captcha) {
        // 生成uuid作业key
        String uuid = UUID.randomUUID().toString();

        String captchaKey = "captcha:verification:" + uuid;

        // 保存验证码到redis中，并设置过期时间
        redisUtils.set(captchaKey, captcha, 300);

        // 封装CaptchaVo返回
        CaptchaVo captchaVo = new CaptchaVo();
        captchaVo.setCaptchaKey(captchaKey);
        captchaVo.setExpire(300);

        return captchaVo;
    }

    @Override
    public String getCaptcha(CaptchaVo captchavo) {
        //通过key获取验证码
        System.out.println("验证码"+captchavo.getCaptchaKey());
        return (String) redisUtils.get(captchavo.getCaptchaKey());
    }
}
